package cn.lysj.framework.elm.service;

import cn.lysj.framework.base.BaseService;
import cn.lysj.framework.common.annotation.CurrentUser;
import cn.lysj.framework.common.domain.User;
import cn.lysj.framework.common.utils.web.ParamUtil;
import cn.lysj.framework.common.utils.web.SelectItem;
import cn.lysj.framework.elm.base.ElmContent;
import cn.lysj.framework.elm.domain.ElmConfig;
import cn.lysj.framework.foodv2.domain.Food;
import cn.lysj.framework.elm.domain.FoodReleveEleme;
import cn.lysj.framework.elm.repository.FoodRelevElemeRepository;
import cn.lysj.framework.foodv2.repository.FoodRepository;
import cn.lysj.framework.elm.vo.FoodRelevElemeVo;
import eleme.openapi.sdk.api.entity.order.OOrder;
import eleme.openapi.sdk.api.entity.product.OItem;
import eleme.openapi.sdk.api.entity.product.QueryPage;
import eleme.openapi.sdk.api.exception.ServiceException;
import eleme.openapi.sdk.api.service.OrderService;
import eleme.openapi.sdk.api.service.ProductService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @ Author ：drj.
 * @ Date  ：Created in 14:54 2019/1/16
 * @ Description:
 **/
@Service
public class FoodRelevElemeService implements BaseService<FoodReleveEleme> {

    @Resource
    private ElmContent elemeContent;

    @Resource
    private FoodRelevElemeRepository foodRelevElemeRepository;

    @Resource
    private FoodRepository foodRepository;


    @Override
    public FoodRelevElemeRepository getRepository() {
        return foodRelevElemeRepository;
    }


    //获取与饿了么未关联的饿了么菜品信息
    public List<SelectItem> getUnRevElmPlatFood(@CurrentUser User user) {
        //获取本店跟了么关联的菜品id
        List<String> list = foodRelevElemeRepository.findplatFormFoodIds(user.getStoreId());
        //获取本店所有菜品信息(id,name)
        List<SelectItem> selectItemList = foodRepository.findFoods(user.getStoreId());
        //过滤
        for (Iterator it = selectItemList.iterator(); it.hasNext(); ) {
            SelectItem selectItem = (SelectItem) it.next();
            if (list.contains(selectItem.getValue())) {
                it.remove();
            }
        }
        return selectItemList;
    }

    //获取未关联饿了么菜品
    public List<SelectItem> getUnReleveElemeFood(@CurrentUser User user) {
        //获取饿了么平台的所有菜品
        List<OItem> oItemList = elemeOItems(user.getStoreId());
        //获取饿了么跟本店关联的菜品id
        List<String> list = foodRelevElemeRepository.findElemeFoodIds(user.getStoreId()); //过滤平台相关的菜品
        if (null == oItemList) {
            return null;
        }
        for (Iterator it = oItemList.iterator(); it.hasNext(); ) {
            OItem oItem = (OItem) it.next();
            if (list.contains(String.valueOf(oItem.getId()))) {
                it.remove();
            }
        }
        List<SelectItem> selectItemList = new LinkedList<>();
        for (OItem oItem : oItemList) {
            SelectItem selectItem = new SelectItem();
            selectItem.setValue(String.valueOf(oItem.getId()));
            selectItem.setName(oItem.getName());
            selectItemList.add(selectItem);
        }
        return selectItemList;
    }


    //获取与饿了么关联的菜品信息
    public List<FoodRelevElemeVo> relevElemeInfo(@CurrentUser User user) {
        elemeContent.cash_items = elemeOItems(user.getStoreId());
        //初始化关联信息集合
        List<FoodRelevElemeVo> foodRelevElemeVoList = new LinkedList<>();
        if(ParamUtil.isBlank(elemeContent.cash_items)){
            return foodRelevElemeVoList;
        }
        //获取门店对应的菜品关系表
        List<FoodReleveEleme> foodRelevElemeList = foodRelevElemeRepository.findByStoreId(user.getStoreId());
        for (FoodReleveEleme foodRelevEleme : foodRelevElemeList) {
            FoodRelevElemeVo foodRelevElemeVo = new FoodRelevElemeVo();
            //关系表id
            foodRelevElemeVo.setFoodRelevElemeId(foodRelevEleme.getId());
            //获取平台菜品名称
            Food food = foodRepository.findOne(foodRelevEleme.getPlatFormFoodId());
            foodRelevElemeVo.setPlatFormFoodName(food.getName());
            for (OItem oItem : elemeContent.cash_items) {
                if (Long.valueOf(foodRelevEleme.getElemeFoodId()).equals(Long.valueOf(oItem.getId()))) {
                    foodRelevElemeVo.setElemeFoodName(oItem.getName());
                    foodRelevElemeVoList.add(foodRelevElemeVo);
                    break;
                }
            }
            //如果没有找到对应的名字,说明菜品信息已经被删除了
            if (ParamUtil.isBlank(foodRelevElemeVo.getElemeFoodName())) {
                foodRelevElemeRepository.delete(foodRelevEleme);
            }
        }
        return foodRelevElemeVoList;
    }

    /*
     * @author drj
     * @date 2019-01-18 19:04
     * @Description
     */
    private List<OItem> elemeOItems(String storeId) {
        if (null == elemeContent.cash_token) {
            return null;
        }
        if (null == elemeContent.cash_token.getRefreshToken()) {
            elemeContent.cash_token = elemeContent.client.getTokenByRefreshToken(elemeContent.cash_token.getRefreshToken());
        }
        ProductService productService = new ProductService(elemeContent.config, elemeContent.cash_token);

        QueryPage queryPage = new QueryPage();
        //查询对应饿了么平台的门店id
        ElmConfig elmConfig=elemeContent.elmConfigService.getRepository().findByStoreId(storeId);
        queryPage.setShopId(Long.valueOf(elmConfig.getShopId()));
        queryPage.setOffset(0L);
        queryPage.setLimit(300L);
        List<OItem> oItemList = new ArrayList<>();
        try {
            oItemList = productService.queryItemByPage(queryPage);
        } catch (ServiceException e) {
            e.printStackTrace();
        }

        return oItemList;
    }


}
